package com.kadron.profile;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.dao.KImageDAO;
import com.kadron.dao.KUserFavoritesDAO;
import com.kadron.dao.KUserImageDAO;
import com.kadron.dao.KUserKadronsDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.entity.User;
import com.kadron.entity.UserImage;
import com.kadron.favorites.KFavoriteItemDataProvider;
import com.kadron.favorites.KFavoriteItemModelCreator;
import com.kadron.favorites.KUserFacoriteSearch;
import com.kadron.manager.KMessageManager;

@AutoCreate
@Name("kProfileManager")
@Scope(ScopeType.PAGE)
public class KProfileManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6059065458765955155L;

	private static final Log log = LogFactory.getLog(KProfileManager.class);
	
	private User user; 
	private UserImage img;
	private Long kadrons;
	
	private KDataModel favoriteList;
	private KFavoriteItemDataProvider favoriteItemDataProvider;
	
	@In
	private KUserFavoritesDAO userFavoritesDAO;
	@In
	private KImageDAO imageDAO;
	@In
	private KUserKadronsDAO userKadronsDAO;
	@In
	private KUserImageDAO userImageDAO;
	
	@Create
	public void init() {
		if (log.isDebugEnabled()) {
			log.debug("Init KProfileManager");
		}
		Long userId = (Long) Component.getInstance("userId");
		if (userId == null || userId <= 0) {
			setError(true);
			return;
		}
		favoriteItemDataProvider = new KFavoriteItemDataProvider(new KFavoriteItemModelCreator(userFavoritesDAO, imageDAO), new KUserFacoriteSearch(userId, 0), userFavoritesDAO);
		kadrons = userKadronsDAO.getUserKadrons(userId);
		user = (User) userFavoritesDAO.find(userId, User.class);
		img = userImageDAO.findByUser(userId);
		searchFav();
	}
	
	public void searchFav() {
		favoriteList = new KDataModel(favoriteItemDataProvider);
	}
	
	public User getUser() {
		return user;
	}

	public UserImage getImg() {
		return img;
	}

	public Long getKadrons() {
		return kadrons;
	}

	public KDataModel getFavoriteList() {
		return favoriteList;
	}
	
}